package com.sjgtw.cloud.web.expert.service.impl;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.expert.domain.ExpertDatabase;
import com.sjgtw.cloud.web.expert.domain.ExpertType;
import com.sjgtw.cloud.web.expert.mapper.ExpertDatabaseMapper;
import com.sjgtw.cloud.web.expert.mapper.ExpertTypeMapper;
import com.sjgtw.cloud.web.expert.service.ExpertDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 专家库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-16
 */
@Service
public class ExpertDatabaseServiceImpl implements ExpertDatabaseService
{
    @Autowired
    private ExpertTypeMapper expertTypeMapper;

    @Autowired
    private ExpertDatabaseMapper expertDatabaseMapper;
    
    @Autowired
    private RedisService redisService;

    /**
     * 查询专家库
     * 
     * @param id 专家库主键
     * @return 专家库
     */
    @Override
    public ExpertDatabase selectExpertDatabaseById(Long id)
    {
        return expertDatabaseMapper.selectExpertDatabaseById(id);
    }

    @Override
    public List<ExpertType> getExpertTypeList() {
        return expertTypeMapper.selectExpertTypeList(new ExpertType());
    }

    @Override
    public int addExpertType(ExpertType expertType) {
        return expertTypeMapper.insertExpertType(expertType);
    }

    @Override
    public int deleteExpertType(Long expertTypeId) {
        return expertTypeMapper.deleteExpertTypeById(expertTypeId);
    }

    /**
     * 查询专家库列表
     * 
     * @param expertDatabase 专家库
     * @return 专家库
     */
    @Override
    public List<ExpertDatabase> selectExpertDatabaseList(ExpertDatabase expertDatabase)
    {
        return expertDatabaseMapper.selectExpertDatabaseList(expertDatabase);
    }

    /**
     * 新增专家库
     * 
     * @param expertDatabase 专家库
     * @return 结果
     */
    @Override
    public int insertExpertDatabase(ExpertDatabase expertDatabase)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        if(user != null){
            expertDatabase.setCreateBy(user.getUsername());
        }

        expertDatabase.setCreateTime(DateUtils.getNowDate());
        return expertDatabaseMapper.insertExpertDatabase(expertDatabase);
    }

    /**
     * 修改专家库
     * 
     * @param expertDatabase 专家库
     * @return 结果
     */
    @Override
    public int updateExpertDatabase(ExpertDatabase expertDatabase)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if(user != null){
            expertDatabase.setUpdateBy(user.getUsername());
        }
        expertDatabase.setUpdateTime(DateUtils.getNowDate());
        return expertDatabaseMapper.updateExpertDatabase(expertDatabase);
    }

    /**
     * 批量删除专家库
     * 
     * @param ids 需要删除的专家库主键
     * @return 结果
     */
    @Override
    public int deleteExpertDatabaseByIds(Long[] ids)
    {
        return expertDatabaseMapper.deleteExpertDatabaseByIds(ids);
    }

    /**
     * 删除专家库信息
     * 
     * @param id 专家库主键
     * @return 结果
     */
    @Override
    public int deleteExpertDatabaseById(Long id)
    {
        return expertDatabaseMapper.deleteExpertDatabaseById(id);
    }
}
